#include "stdafx.h"

void LabelButton::update_graphical()
{
	if (ffm)
	{
		delete ffm;
	}
	if (font)
	{
		delete font;
	}

	ffm = new Gdiplus::FontFamily(fontname.c_str());
	font = new Gdiplus::Font(ffm, fontsize);
}

void LabelButton::update_length()
{
	Gdiplus::PointF origin{ 0,0 };
	Gdiplus::RectF rc;

	graphics->MeasureString(left_append.c_str(), -1, font, origin, &rc);
	length_left = rc.Width;

	graphics->MeasureString(caption.c_str(), -1, font, origin, &rc);
	length_middle = rc.Width;
	height_text = rc.Height;

	graphics->MeasureString(right_append.c_str(), -1, font, origin, &rc);
	length_right = rc.Width;

	if (valid_region)
	{
		delete valid_region;
	}
	determine_valid_region();
}

LabelButton::LabelButton(int left, int top)
{
	this->left = left;
	this->top = top;
	brush_text = new Gdiplus::SolidBrush(color_text);

	update_graphical();
}

LabelButton::~LabelButton()
{
	delete brush_text;

	if (ffm)
	{
		delete ffm;
	}
	if (font)
	{
		delete font;
	}
	if (valid_region)
	{
		delete valid_region;
	}
}

bool LabelButton::MouseMove(int button, int x, int y)
{
	state = STATE_MOVE;

	mouse_move(button, x, y);

	return true;
}

bool LabelButton::MouseLeave(int button, int x, int y)
{
	state = STATE_NORMAL;

	mouse_leave(button, x, y);

	return true;
}

void LabelButton::set_left_append(std::wstring append)
{
	left_append = append;
	update_length();
}

void LabelButton::set_right_append(std::wstring append)
{
	right_append = append;
	update_length();
}

void LabelButton::set_caption(std::wstring caption)
{
	this->caption = caption;
	update_length();
}

void LabelButton::set_fontname(std::wstring fontname)
{
	this->fontname = fontname;
	update_graphical();
}

void LabelButton::set_fontsize(int fontsize)
{
	this->fontsize = fontsize;
	update_graphical();
}

void LabelButton::draw(Gdiplus::Graphics & graphics)
{
	Gdiplus::PointF origin{ 0,0 };

	//if (!this->graphics)
	//{
		update_length();
		determine_valid_region();
	//}

	switch (state)
	{
	case STATE_NORMAL:
		origin.X = left;
		origin.Y = top - height_text / 2.0;
		graphics.DrawString(caption.c_str(), -1, font, origin, brush_text);
		break;
	case STATE_MOVE:
		{
			//std::wstring temp;
			//temp = left_append + caption + right_append;
			origin.X = left - length_left;
			origin.Y = top - height_text / 2.0;
			graphics.DrawString(left_append.c_str(), -1, font, origin, brush_text);
			origin.X = left;
			graphics.DrawString(caption.c_str(), -1, font, origin, brush_text);
			origin.X = left + length_middle - 8;
			graphics.DrawString(right_append.c_str(), -1, font, origin, brush_text);
			break;
		}
	}
}

void LabelButton::determine_valid_region()
{
	valid_region = new RectShape(left, top - height_text / 2.0, length_middle, height_text);
}